using System.Text.RegularExpressions;

namespace UniversalAdminSystem.Domian.PermissionManagement.ValueObjects;

public sealed partial class PermissionName
{
    public string Value { get; private set; } = null!;

    private PermissionName(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new ArgumentException("权限名称不能为空", nameof(value));
        }

        if (!MyRegex().IsMatch(value))
        {
            throw new ArgumentException("权限名称只能包含中文、英文、数字、下划线和连字符", nameof(value));
        }

        if (value.Length > 20)
        {
            throw new ArgumentException("权限名称长度不能超过20个字符", nameof(value));
        }

        Value = value;
    }

    /// <summary>
    /// 正则表达式：格式校验规则
    /// </summary>
    /// <returns></returns>
    [GeneratedRegex(@"^[\p{IsCJKUnifiedIdeographs}a-zA-Z0-9_-]{1,20}$")]
    private static partial Regex MyRegex();

    /// <summary>
    /// 工厂方法
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static PermissionName Create(string value) => new(value);

    /// <summary>
    /// 隐式转换为string
    /// </summary>
    /// <param name="name"></param>
    public static implicit operator string(PermissionName name) => name.Value;

    /// <summary>
    /// 显式转换为PermissionName，带验证逻辑
    /// </summary>
    /// <param name="value"></param>
    public static explicit operator PermissionName(string value) => Create(value);
}